#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @author: zhangbo

# @time: 2025/3/24 14:55

import os
import time

class StartExe:

    def start_exe_file(self, exe_path=None, sleep_time=60):
        """
        启动模拟器
        :param exe_path: 模拟器exe文件路径
        :param sleep_time: 进程运行时间（秒）
        :return:
        """

        if exe_path:

            # 启动exe文件
            os.system(f'start "" "{exe_path}"')

            # 模拟器启动时间
            start_time = time.time()

            # 等待指定时间
            time.sleep(sleep_time)

            # 获取进程的 PID
            process_name = 'JTDVR.exe'
            pid_command = f'tasklist | findstr {process_name}'
            result = os.popen(pid_command).read()

            # 解析 PID
            if result:
                pid = result.split()[1]
                try:
                    os.system(f'taskkill /F /PID {pid}')  # 使用 taskkill 命令关闭进程

                    # 模拟器关闭时间
                    end_time = time.time()

                    return start_time, end_time
                except Exception as e:
                    print(f"关闭进程时发生错误: {e}")
            else:
                print("未找到进程")

                return start_time, None
